#ifndef __CAN_DRV_H_
#define __CAN_DRV_H_

#include "types_local.h"
#include "xmc_can.h"
#include "../canmsg/canmsg.h"

#define CAN_11_BIT_MASK 0x7ffU
#define CAN_29_BIT_MASK 0x1fffffff

#define CAN_BUFFER_SIZE                     2                       /* Depth of receive buffer */
#define NO_OF_CAN_MAILBOXES                 64                      /* No of mailboxes supported by the hardware, or the number s/w supports */
#define MESSAGE_RECEIVED_BUFFER_SIZE        20U                     /* Received message mailbox FIFO depth */
#define NO_OF_TRANSMIT_BUFFERS              32                      /* Transmit FIFO depth */

#define CAN_PORT_INT                        0                       /* Indicate CAN port 0 exists */
#define CAN_PORT_EXT                        1                       /* Indicate CAN port 1 exists */
#define NO_OF_CAN_PORTS                     3                       /* CAN NODE0 and CAN NODE1 */


typedef struct
{
    u16 in_ptr;
    u16 out_ptr;
    can_mailbox mailbox[CAN_BUFFER_SIZE];
} can_message_buffer;


typedef struct
{
    u8 port;
    u8 instance;                            // instance number of particular function assigned to mailbox
    can_message_buffer buffer;
    u16 handler;                            // May be more useful to store the function id rather than pointer to 
} can_message_handler;

extern can_message_handler can_message[];

typedef struct
{
    u16 in_ptr;
    u16 out_ptr;

    can_mailbox buffer[NO_OF_TRANSMIT_BUFFERS];
} can_transmit_messages;

typedef struct
{
	u16 in_ptr;
	u16 out_ptr;

    u16 mail_box[MESSAGE_RECEIVED_BUFFER_SIZE];
} can_receive_messages;

extern can_receive_messages can_received_messages;
extern can_transmit_messages can_transmit_message[];
extern can_mailbox *current_message;

extern s16 assign_a_mailbox(s16 port);
extern s16 send_can_message(can_mailbox *mailbox);
extern void process_can_received_messages_tic(void);

#endif
